#include "ObjectPch.h"
#include "ObjectMesh.h"
#include "VertexType.h"
#include "RenderFacade.h"
#include "XmlUtil.h"
#include "StringParseUtil.h"

ObjectMesh::ObjectMesh(void)
: m_pVertices( NULL ),
m_pIndices( NULL ),
m_pIB( NULL ),
m_pVB( NULL ),
m_uVertexCount( 0 ),
m_uIndexCount( 0 )
{
}

ObjectMesh::~ObjectMesh(void)
{
	UnInit();
}

void ObjectMesh::Update( const float& fTimeDelta )
{
}

void ObjectMesh::Render()
{
	if( RenderFacade::GetInstance() )
	{
		RenderFacade::GetInstance()->SetStreamSource( 0, m_pVB, 0, sizeof( VertexPos3ND ) );
		RenderFacade::GetInstance()->SetFVF( VertexPos3ND::GetFVF() );
		RenderFacade::GetInstance()->SetIndices( m_pIB );
		RenderFacade::GetInstance()->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0, 0, m_uVertexCount, 0, m_uIndexCount / 3 );
	}
}

void ObjectMesh::Init( const int& vertexcount, const VertexPos3ND* vertexlist, const int& indexcount, const WORD* indexlist )
{
	if( !m_pVertices )
	{
		m_uVertexCount = vertexcount;
		m_pVertices = new VertexPos3ND[ vertexcount ];
		memcpy_s( m_pVertices, sizeof( VertexPos3ND ) * vertexcount, vertexlist, sizeof( VertexPos3ND ) * vertexcount );
	}

	if( !m_pIndices )
	{
		m_uIndexCount = indexcount;
		m_pIndices = new WORD[ indexcount ];
		memcpy_s( m_pIndices, sizeof( WORD ) * indexcount, indexlist, sizeof( WORD ) * indexcount );
	}

	CreateVB();
	CreateIB();

}

void ObjectMesh::UnInit()
{
	SAFE_RELEASE( m_pVB );
	SAFE_RELEASE( m_pIB );

	SAFE_DELETE_ARRAY( m_pVertices );
	SAFE_DELETE_ARRAY( m_pIndices );
}


void ObjectMesh::Load( const char* FileName )
{
	std::vector< float >	vecVertexList;

	XmlDocument xmlDoc;
	if( xmlDoc.LoadFile( FileName ) )
	{
		XmlNode* pRootNode = xmlDoc.GetChildNode( "IGame" );
		if( pRootNode )
		{
			 XmlNode* pChildNode = pRootNode->GetChildNode( "Node" );
			 if( pChildNode )
			 {
				 XmlNode* pMeshNode = pChildNode->GetChildNode( "Mesh" );
				 if( pMeshNode )
				 {
					 XmlNode* pVertNode = pMeshNode->GetChildNode( "Vertices" );
					 if( pVertNode )
					 {
						 int nVertCount = 0;
						 FormatedNumberList VertexCount( pVertNode->GetAttribute( "Count" ), "d" );
						 VertexCount.Copy( (int*)&nCount );

						 FormatedNumberList Vertices( pVertNode->GetText(), "f" );

						 VertexPos3DNormal* verticeslist = new VertexPos3DNormal[ nVertCount ];
						 Vertices.Copy( (VertexPos3DNormal*)verticeslist );
					 }

					 XmlNode* pNormalNode = pMeshNode->GetChildNode( "Normals" );
					 if( pNormalNode )
					 {
						 int nNormalCount = 0;

						 FormatedNumberList NormalCount( pNormalNode->GetAttribute( "Count" );
						 NormalCount.Copy( (int*)&nNormalCount );

						 Vector3* vecNormal = new Vector3[ nNormalCount ];
						 FormatedNumberList Normals( pNormalNode->GetText(), "f" );
						 Normals.Copy( (Vector3*)vecNormal );

					 }
				 }
			 }
		}
	}
}


void ObjectMesh::CreateVB()
{
	HRESULT hr = RenderFacade::GetInstance()->CreateVertexBuffer( sizeof( VertexPos3ND ) * m_uVertexCount, 0, VertexPos3ND::GetFVF(), D3DPOOL_DEFAULT, &m_pVB, NULL ); 
	if( SUCCEEDED( hr ) )
	{
		void* pVertices = NULL;
		if( SUCCEEDED( m_pVB->Lock( 0, sizeof( VertexPos3ND ) * m_uVertexCount, (void**)&pVertices, 0 ) ) )
		{
			memcpy_s( pVertices, sizeof( VertexPos3ND ) * m_uVertexCount, m_pVertices, sizeof( VertexPos3ND ) * m_uVertexCount );
		}
		m_pVB->Unlock();
	}
}

void ObjectMesh::CreateIB()
{
	HRESULT hr = RenderFacade::GetInstance()->CreateIndexBuffer( sizeof( WORD ) * m_uIndexCount, D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &m_pIB, NULL );
	if( SUCCEEDED( hr ) )
	{
		void* pIndices = NULL;
		if( SUCCEEDED( m_pIB->Lock( 0, sizeof( WORD ) * m_uIndexCount, (void**)&pIndices, 0 ) ) )
		{
			memcpy_s( pIndices, sizeof( WORD ) * m_uIndexCount, m_pIndices, sizeof( WORD ) * m_uIndexCount );
		}
		m_pIB->Unlock();
	}
}
